<template>
  <a-card :bordered='false'>

    <!-- 查询区域 -->
    <div class='table-page-search-wrapper'>
      <a-form layout='inline' @keyup.enter.native='mainSearch'>
        <a-row :gutter='24'>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <a-form-item label='调整单号'>
              <a-input placeholder='请输入调整单号' v-model='mainQueryParams.adjustNo'></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <a-form-item label='入库单号'>
              <a-input placeholder='请输入入库单号' v-model='mainQueryParams.no'></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <a-form-item label='状态'>
              <a-select v-model='mainQueryParams.state' placeholder='请选择状态'>
                <a-select-option v-for='(d,i) in stateList' :key='d.id' :value='d.id'>{{ d.value }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if='toggleSearchStatus'>
            <a-col :xl='6' :lg='7' :md='8' :sm='24'>
              <a-form-item label='出库时间'>
                <a-range-picker :style="{width: '100%'}" format='YYYY-MM-DD'
                                :placeholder="['开始时间', '结束时间']"
                                v-model='mainRangePikerDate'
                                @change='mainDateChange' />
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="mainSearch" icon="search">查询</a-button>
              <a-button type="primary" @click="mainSearchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class='table-operator'>
<!--      <a-button @click='mainAdjustAdd' type='primary' icon='plus'>新增</a-button>-->
      <a-button type='primary' icon='download' @click="handleExportXls('批量入库调整主表')">导出</a-button>
      <a-upload name='file' :showUploadList='false' :multiple='false' :headers='tokenHeader' :action='importExcelUrl'
                @change='handleImportExcel'>
<!--        <a-button type='primary' icon='import'>导入</a-button>-->
      </a-upload>
      <a-dropdown v-if='selectedRowKeys.length > 0'>
        <a-menu slot='overlay'>
          <a-menu-item key='1' @click='mainBatchDelete(selectedRowKeys)'>
            <a-icon type='delete' />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style='margin-left: 8px'> 批量操作
          <a-icon type='down' />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class='ant-alert ant-alert-info' style='margin-bottom: 16px;'>
        <i class='anticon anticon-info-circle ant-alert-icon'></i>
        <span>已选择</span>
        <a style='font-weight: 600'>
          {{ selectedRowKeys.length }}
        </a>
        <span>项</span>
        <a style='margin-left: 24px' @click='onClearSelected'>清空</a>
      </div>

      <a-table
        ref='table'
        size='middle'
        bordered
        rowKey='id'
        :columns='columns'
        :dataSource='mainDataSource'
        :pagination='mainPagination'
        :loading='mainLoading'
        :scroll='{ x: true }'
        :rowSelection='{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}'
        @change='mainPaginationChange'>

        <span slot='action' slot-scope='text, record'>
          <a-button v-if='record.state===0' @click='mainAdjustEdit(record)' style='display: inline-block' type="danger">财务审核</a-button>
          <a-button v-if='record.state===1' @click='mainAdjustEdit(record)' style='display: inline-block' type="danger">撤销财务审核</a-button>

          <a-divider type='vertical' />
          <a @click='mainDetail(record)'>详情</a>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <!--    <tFlowerAdjustTagInMain-modal ref="modalForm" @ok="modalFormOk" />-->
    <!--第一个弹窗（选择要调整的批量入库单的弹窗）-->
    <a-modal
      title='入库单选择（请选择要调整的入库单，然后点击确定按钮）'
      width='80%'
      :visible='firstVisible'
      @ok='firstModalOk'
      @cancel='firstModalCancel'
    >
      <!-- 查询区域 -->
      <div class='table-page-search-wrapper'>
        <a-form layout='inline' @keyup.enter.native='firstSearch'>
          <a-row :gutter='24'>
            <a-col :xl='6' :lg='7' :md='8' :sm='24'>
              <a-form-item label='入库单号'>
                <a-input placeholder='请输入入库单号' v-model='firstQueryParams.no'></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl='6' :lg='7' :md='8' :sm='24'>
              <a-form-item label='基地'>
                <a-select v-model='firstQueryParams.sourceBase' placeholder='请选择基地' @change='sourceBaseChange'>
                  <a-select-option v-for='(d,i) in sourceBaseList' :key='d.id' :value='d.id'>{{ d.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl='6' :lg='7' :md='8' :sm='24'>
              <a-form-item label='地块'>
                <a-select v-model='firstQueryParams.sourceGreenhouse' placeholder='请选择地块'>
                  <a-select-option v-for='(d,i) in sourceGreenhouseList' :key='d.id' :value='d.id'>{{
                      d.name
                    }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if='toggleSearchStatus'>
              <a-col :xl='6' :lg='7' :md='8' :sm='24'>
                <a-form-item label='冷库'>
                  <a-select v-model:value='firstQueryParams.storehouse' style='width: 100%;' placeholder='请选择冷库'>
                    <a-select-option v-for='d in storehouseList' :key='d.id' :value='d.code'>{{ d.name }}
                    </a-select-option>
                  </a-select>

                </a-form-item>
              </a-col>
              <a-col :xl='6' :lg='7' :md='8' :sm='24'>
                <a-form-item label='分拣日期'>
                  <a-range-picker :style="{width: '100%'}" format='YYYY-MM-DD'
                                  :placeholder="['开始时间', '结束时间']"
                                  @change='firstModalDateChange' />
                </a-form-item>
              </a-col>
            </template>
            <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <span style='float: left;overflow: hidden;' class='table-page-search-submitButtons'>
              <a-button type='primary' @click='firstSearch' icon='search'>查询</a-button>
              <a-button type='primary' @click='firstSearchReset' icon='reload' style='margin-left: 8px'>重置</a-button>
              <a @click='handleToggleSearch' style='margin-left: 8px'>
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!-- table区域-begin -->
      <div>
        <a-table
          ref='table'
          size='middle'
          bordered
          rowKey='id'
          class='j-table-force-nowrap'
          :columns='firstColumns'
          :dataSource='firstDataSource'
          :pagination='firstPagination'
          :loading='firstLoading'
          :scroll='{ x: true }'
          :rowSelection="{selectedRowKeys: firstSelectedRowKeys, type:'radio',onChange: firstSelectChange}"
          @change='firstPaginationChange'>
        </a-table>
      </div>
    </a-modal>
    <!--第二个弹窗（新增或编辑调整单的弹窗）-->
    <a-modal v-model:visible='secondVisible' width='80%' :title='secondModalTitle'
             :maskClosable='false'>
      <div>
        <a-row>
          <a-col :span='6'>
            <div style='display: inline-block;color: red;'></div>
            <div style='display: inline-block'>调整单号：</div>
            <div style='display: inline-block;'>
              <input v-model='adjustOrder.adjustNo' disabled placeholder='自动生成' />
            </div>
          </a-col>
          <a-col :span='6'>
            <div style='display: inline-block;color: red;'></div>
            <div style='display: inline-block'>入库单号：</div>
            <div style='display: inline-block;color: red'>
              {{ adjustOrder.no }}
            </div>
          </a-col>
          <a-col :span='8'>
            <div style='display: inline-block;'>账期：</div>
            <div style='display: inline-block;color: red'>
              {{ adjustOrder.period }}
            </div>
          </a-col>
        </a-row>
        <a-row style='margin-top: 10px'>
          <a-col :span='6'>
            <div style='display: inline-block'>基地：</div>
            <div style='display: inline-block;color: red'>
              {{ adjustOrder.sourceBase_dictText }}
            </div>
          </a-col>
          <a-col :span='6'>
            <div style='display: inline-block'>地块：</div>
            <div style='display: inline-block;color: red'>
              {{ adjustOrder.sourceGreenhouse_dictText }}
            </div>
          </a-col>
          <a-col :span='6'>
            <div style='display: inline-block'>冷库：</div>
            <div style='display: inline-block;color: red'>
              {{ adjustOrder.storehouse }}
            </div>
          </a-col>
          <a-col :span='6'>
            <div style='display: inline-block'>分拣日期：</div>
            <div style='display: inline-block;color: red'>
              {{ adjustOrder.sortTime }}
            </div>
          </a-col>
        </a-row>
      </div>
      <a-divider />
      <div style='margin-top: -20px'>
        <span style='font-size: 20px;color: #0742f3;margin-bottom: 3px;margin-right: 30px;'>入库调整产品</span>
<!--        <span><a-button type='primary' icon='plus' @click='varietyModalShow'>新增</a-button> </span>-->
        <div style='margin-top: 7px'>
          <a-table :columns='secondColumns' :data-source='secondDataSource'
                   bordered row-key='uuid'
                   :loading='secondLoading'>

            <template v-for="tagNums in ['tagNum']" :slot='tagNums' slot-scope='text, record, index'>
              <div :key='tagNums' style='float: left;margin-right: 0'>
                <input v-model:value='record.tagNum' placeholder='请输入入库数量' disabled />
              </div>
            </template>
            <template slot='operation' slot-scope='text, record'>
              <a-popconfirm v-if='secondDataSource.length' title='确定删除?'
                            @confirm='() => varietyDelete(record.varietyCode)'>
                <a style='display: none'>删除</a>
              </a-popconfirm>
            </template>
          </a-table>
        </div>
      </div>
      <template slot='footer'>
        <a-button key='back' @click='secondVisible=false'>
          关闭
        </a-button>
        <a-button v-if='adjustOrder.state===0&&!secondModalDisable' key='submit' type="danger" :loading='secondModalFinanceSureButtonLoading'
                  @click='secondModalFinanceSure'>
          财务审核
        </a-button>
        <a-button v-if='adjustOrder.state===1&&!secondModalDisable' key='submit' type="danger" :loading='secondModalFinanceSureButtonLoading'
                  @click='secondModalFinanceCancelSure'>
          撤销财务审核
        </a-button>
      </template>
    </a-modal>
    <!--第三个弹窗（选择商品的弹窗）-->
    <a-modal v-model:visible='varietyVisible' width='60%' title='产品选择' @ok='varietyAdd'>
      <div style='margin-bottom: 10px'>
        <a-row style='margin-top: 10px'>
          <a-col :span='8'>
            <div style='display: inline-block;width: 45px;'>产品：</div>
            <div style='display: inline-block;width: 70%'>
              <a-input v-model='queryVarietyParams.name' placeholder='请输入产品名称' allow-clear
                       @change='searchFlowerVarietys' />
            </div>
          </a-col>
          <a-col :span='10'>
            <div style='display: inline-block;width: 80px;'>商品编码：</div>
            <div style='display: inline-block;width: 70%'>
              <a-input v-model='queryVarietyParams.code' placeholder='请输入商品编码' allow-clear
                       @change='searchFlowerVarietys' />
            </div>
          </a-col>
          <a-col :span='4'>
            <a-button type='primary' icon='search' @click='searchFlowerVarietys'>
              查询
            </a-button>
          </a-col>
        </a-row>
      </div>
      <a-table :columns='selectVarietyColumns' :data-source='tagInMain2DataSource' bordered
               :loading='selectVarietyLoading'
               :row-selection="{selectedRowKeys: varietySelectedRowKeys,type: 'checkbox',onChange:onVarietySelectChange,onSelect:varietyOnSelect,
               onSelectAll:varietyOnSelectAll}"
               row-key='uuid'
               :scroll='{y:280}'
               :pagination='varietyPagination'
               @change='varietyPaginationChange'
      ></a-table>
    </a-modal>
  </a-card>
</template>

<script>

import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import TFlowerAdjustTagInMainModal from './modules/TFlowerAdjustTagInMainModal'
import {
  getFlowerTagInMain2,
  getFlowerTagInMain2DetailByMainId,
  flowerTagInMain2Add,
  flowerTagInMain2Edit,
  flowerTagInMain2Delete,
  flowerTagInMain2DeleteBatch,
  getVarietyList,
  queryFlowerBase,
  queryGreenhouseByPd,
  getFlowerStorehouseDict,
  getFlowerCost,
  period,
  getFlowerAdjustTagInMain,
  getFlowerAdjustTagInMainDetailByMainId,
  flowerAdjustTagInMainAdd,
  flowerAdjustTagInMainEdit,
  flowerAdjustTagInMainDelete,
  flowerAdjustTagInMainDeleteBatch,
  flowerAdjustTagInMainFinanceSure,
  flowerAdjustTagInMainFinanceCancelSure,
} from '@/api/api'
import moment from 'moment'
import { ifInteger, ifNumber1 } from '../../utils/ifNumber'
import { getUuid } from '../../utils/uuid'

export default {
  name: 'TflowerAdjustTagInMainFinanceSure',
  mixins: [JeecgListMixin],
  components: {
    TFlowerAdjustTagInMainModal
  },
  data() {
    return {
      description: '批量入库调整主表管理页面',
      stateList: [{ id: '', value: '全部' }, { id: '0', value: '待财务审核' },
        { id: 1, value: '待库管审核' }, {id: 2,value: '待运管审核' },{id: 3,value: '运管已审核' },
      ],
      //主页面查询的条件
      mainQueryParams: {
        no: '',//入库单号
        adjustNo: '',//调整单号
        state:'',//状态
        sDate: '',//开始时间
        eDate: ''//结束时间
      },
      mainRangePikerDate:[],
      //主页面数据源
      mainDataSource: [],
      // 主页面表格表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '调整单号',
          align: 'center',
          dataIndex: 'adjustNo',
          width: 140
        },
        {
          title: '入库单号',
          align: 'center',
          dataIndex: 'no',
          width: 120
        },

        {
          title: '基地',
          align: 'center',
          dataIndex: 'sourceBase_dictText',
          width: 100
        },
        {
          title: '地块',
          align: 'center',
          dataIndex: 'sourceGreenhouse_dictText',
          width: 100
        },
        {
          title: '库房',
          align: 'center',
          dataIndex: 'storehouse',
          width: 100
        },
        {
          title: '成本中心',
          align: 'center',
          dataIndex: 'cost',
          width: 100
        },
        {
          title: '分拣日期',
          align: 'center',
          dataIndex: 'sortTime',
          width: 100
        },
        {
          title: '账期号',
          align: 'center',
          dataIndex: 'period',
          width: 100
        },
        /*{
          title: '账期状态',
          align: 'center',
          dataIndex: 'periodState',
          width: 100
        },*/
        {
          title: '财务审核人',
          align: 'center',
          dataIndex: 'financeSureUser',
          width: 100
        },
        {
          title: '财务审核时间',
          align: 'center',
          dataIndex: 'financeSureTime',
          width: 130
        },
        {
          title: '库管审核人',
          align: 'center',
          dataIndex: 'storekeeperSureUser',
          width: 100
        },
        {
          title: '库管审核时间',
          align: 'center',
          dataIndex: 'storekeeperSureTime',
          width: 130
        },
        {
          title: '运管审核人',
          align: 'center',
          dataIndex: 'operationSureUser',
          width: 100
        },
        {
          title: '运管审核时间',
          align: 'center',
          dataIndex: 'operationSureTime',
          width: 130
        },
        {
          title: '状态',
          align: 'center', customRender: function(text) {
            let stateString = ''
            switch (text) {
              case 0:
                stateString = '录入'
                break
              case 1:
                stateString = '财务审核'
                break
              case 2:
                stateString = '库管审核'
                break
              case 3:
                stateString = '运管审核'
                break
              default:
                stateString = '未知'
            }
            return stateString
          },
          dataIndex: 'state',
          width: 100
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width: 200,
          fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      mainPagination: {//主页面表格的分页
        total: 0,
        current: 1,
        pageSize: 10,//每页中显示10条数据
        showSizeChanger: true,
        pageSizeOptions: ['10', '20', '50', '100'],//每页中显示的数据
        showTotal: total => `共有 ${total} 条数据`  //分页中显示总的数据
      },
      //主页面的表格是否处于加载状态
      mainLoading: false,
      // 请求参数
      url: {
        list: '/flowerAdjust/tFlowerAdjustTagInMain/list',
        delete: '/flowerAdjust/tFlowerAdjustTagInMain/delete',
        deleteBatch: '/flowerAdjust/tFlowerAdjustTagInMain/deleteBatch',
        exportXlsUrl: 'flowerAdjust/tFlowerAdjustTagInMain/exportXls',
        importExcelUrl: 'flowerAdjust/tFlowerAdjustTagInMain/importExcel'
      },
      firstVisible: false,//是否打开第一个的弹窗
      firstQueryParams: {// 第一个弹窗中查询的条件
        no: '',//入库单号
        sourceBase: '',//基地
        sourceGreenhouse: '',//地块
        storehouse: '',//冷库
        sDate: '',//开始时间
        eDate: ''//结束时间
      },
      firstDataSource: [],//第一个弹窗的表格数据
      firstPagination: {//第一个弹窗表格的分页
        total: 0,
        current: 1,
        pageSize: 10,//每页中显示10条数据
        showSizeChanger: true,
        pageSizeOptions: ['10', '20', '50', '100'],//每页中显示的数据
        showTotal: total => `共有 ${total} 条数据`  //分页中显示总的数据
      },
      sourceBaseList: [],//基地列表
      sourceGreenhouseList: [],//地块列表
      storehouseList: [],//冷库列表
      // 第一个弹窗中的表头
      firstColumns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '入库单号',
          align: 'center',
          width: 100,
          dataIndex: 'no'
        },
        {
          title: '账期',
          align: 'center',
          width: 100,
          dataIndex: 'period'
        }, {
          title: '基地',
          align: 'center',
          width: 120,
          dataIndex: 'sourceBase_dictText'
        },
        {
          title: '地块',
          align: 'center',
          width: 120,
          dataIndex: 'sourceGreenhouse_dictText'
        },
        {
          title: '成本中心',
          align: 'center',
          width: 120,
          dataIndex: 'cost_dictText'
        }, {
          title: '冷库',
          align: 'center',
          width: 120,
          dataIndex: 'storehouse_dictText'
        },
        {
          title: '分拣日期',
          align: 'center',
          width: 100,
          dataIndex: 'sortTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '创建人',
          align: 'center',
          dataIndex: 'createBy_dictText'
        },
        {
          title: '创建日期',
          align: 'center',
          width: 120,
          dataIndex: 'createTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        }
      ],
      firstLoading: false,//第一个弹窗表格的加载状态
      firstSelectedRowKeys: [],//第一个弹窗中表格的所选择订单的key列表
      firstSelectionRows: [],//第一个弹窗中表格的所选择的订单列表
      adjustOrder: {},////第一个弹窗中表格的所选择的订单
      tagInMain2DataSource: [],//第一个弹窗中所选择的批量入库单的子表的数据
      periodList: [],//从后台取回的账期列表
      dateFormat: 'YYYY-MM-DD',
      defaultTime: '',
      secondVisible: false,//是否打开第二个的弹窗
      secondModalTitle: '',
      addOrEditFlag: 'add',//打开第二个的弹窗是新增还是编辑的标识
      secondLoading: false,//第二个弹窗表格的加载状态
      secondColumns: [// 第二个弹窗的表头
        {
          title: '采收产品',
          dataIndex: 'variety'
        }, {
          title: '商品编码',
          dataIndex: 'varietyCode'
        }, {
          title: '打包规格',
          dataIndex: 'packingSpecifications'
        }, {
          title: '单位',
          dataIndex: 'flowerPoleLength'
        }, {
          title: '入库数量',
          dataIndex: 'tagNum',
          scopedSlots: { customRender: 'tagNum' }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: 150,
          scopedSlots: { customRender: 'operation' }
        }
      ],
      secondDataSource: [],//第二个弹窗的表格数据
      secondModalDisable: false,//第二个弹窗中按钮是否可见
      secondModalFinanceSureButtonLoading: false,//第二个弹窗中财务审核或撤销按钮的加载状态
      varietyVisible: false,//是否打开第三个弹窗(商品选择的弹窗)
      selectVarietyColumns: [//第三个弹窗(商品选择的弹窗)的表头
        {
          title: '产品',
          dataIndex: 'variety'
        }, {
          title: '商品编码',
          dataIndex: 'varietyCode'
        }, {
          title: '规格',
          dataIndex: 'packingSpecifications'
        }, {
          title: '单位',
          dataIndex: 'flowerPoleLength'
        }
      ],
      selectVarietyDataSource: [],//第三个弹窗(商品选择的弹窗)中表格的数据源
      selectVarietyLoading: false,//第三个弹窗(商品选择的弹窗)中表格的加载状态
      selectVariety: [],// 第三个弹窗(商品选择的弹窗)中已选择的商品
      varietyPagination: {// 第三个弹窗(商品选择的弹窗)中表格的分页
        total: 0,
        current: 1,
        pageSize: 10,//每页中显示10条数据
        showSizeChanger: false,
        showQuickJumper: true,
        showTotal: total => `共有 ${total} 条数据`  //分页中显示总的数据
      },
      queryVarietyParams: {// 第三个弹窗(商品选择的弹窗)中的查询
        name: '',
        code: ''
      },
      varietySelectedRowKeys: [],//第三个弹窗(商品选择的弹窗)中表格的所选择商品的key列表
      varietySelectionRows: []//第三个弹窗(商品选择的弹窗)中表格的所选择的商品列表
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
    this.storehouseList = JSON.parse(localStorage.getItem('ch_storehouse') ? localStorage.getItem('ch_storehouse') : '')
  },
  mounted() {
    this.queryFlowerAdjustTagInMain()
    this.queryFlowerBase()
    this.getPeriod()
  },
  methods: {
    moment,
    // 主页面的查询
    mainSearch() {
      this.mainPagination.current = 1
      this.queryFlowerAdjustTagInMain()
    },
    //主页面重置查询
    mainSearchReset() {
      this.mainQueryParams = {
        no: '',//入库单号
        adjustNo: '',//调整单号
        state:'',//状态
        sDate: '',//开始时间
        eDate: ''//结束时间
      },
        this.mainSearch()
    },
    //主页面日期段选择改变时的回调
    mainDateChange(date, dateString) {
      this.mainQueryParams.sDate = dateString[0]
      this.mainQueryParams.eDate = dateString[1]
    },
    //查询主页面表格的数据
    queryFlowerAdjustTagInMain() {
      this.mainLoading = true
      let params = {
        pageNo: this.mainPagination.current,
        pageSize: this.mainPagination.pageSize
      }
      params = { ...this.mainQueryParams, ...params }
      getFlowerAdjustTagInMain(params).then(res => {
        if (res.success) {
          this.mainDataSource = res.result.records
        } else {
          this.$message.error(res.message)
        }
        this.mainLoading = false
      })
    },
    // 主页面点击页码的回调
    mainPaginationChange(pagination) {
      this.mainPagination.current = pagination.current
      this.mainPagination.pageSize = pagination.pageSize
      this.queryFlowerAdjustTagInMain()
    },
    //主页面点击新增按钮
    mainAdjustAdd() {
      this.firstSelectedRowKeys.length = 0
      this.firstSelectionRows.length = 0
      this.secondDataSource.length=0
      this.firstModalQuery()
      this.firstVisible = true
    },
    //主页面点击财务审核或撤销财务审核按钮
    mainAdjustEdit(record) {
      this.adjustOrder = record
      this.addOrEditFlag = 'edit'
      this.secondModalTitle = '编辑批量入库调整单'
      this.secondDataSource.length = 0
      this.secondModalQuery(record.id)
      this.secondModalDisable = false
      this.secondVisible = true
    },
    //主页面中的点击详情
    mainDetail(record){
      this.adjustOrder = record
      this.addOrEditFlag = 'edit'
      this.secondModalTitle = '编辑批量入库调整单'
      this.secondDataSource.length = 0
      this.secondModalQuery(record.id)
      this.secondModalDisable = true
      this.secondVisible = true
    },
    //主页面删除单条订单
    mainDelete(id) {
      flowerAdjustTagInMainDelete({ id: id }).then(res => {
        if (res.success) {
          this.queryFlowerAdjustTagInMain()
          this.$message.success('删除成功')
        } else {
          this.$message.error(res.message)
        }
      })
    },
    //主页面删除单条订单
    mainBatchDelete(selectedRowKeys) {
      if (selectedRowKeys.length > 0) {
        let ids = ''
        for (let item of selectedRowKeys) {
          ids = ids + item + ','
        }
        flowerAdjustTagInMainDeleteBatch({ ids: ids }).then(res => {
          if (res.success) {
            this.queryFlowerAdjustTagInMain()
            this.$message.success('删除成功')
          } else {
            this.$message.error(res.message)
          }
        })
      }
    },
    //获取基地
    queryFlowerBase() {
      queryFlowerBase().then(res => {
        if (res.success) {
          this.sourceBaseList = res.result
        } else {
          this.$message.error(res.message)
        }
      })
    },
    //获取地块
    queryGreenhouseByPd(baseId) {
      queryGreenhouseByPd(baseId).then(res => {
        if (res.success) {
          this.sourceGreenhouseList = res.result
        } else {
          this.$message.error('未找到对应的地块')
        }
      })
    },
    //选择基地的Change
    sourceBaseChange(e) {
      this.queryGreenhouseByPd(e)
    },
    //第一个弹窗日期段选择改变时的回调
    firstModalDateChange(date, dateString) {
      this.firstQueryParams.sDate = dateString[0]
      this.firstQueryParams.eDate = dateString[1]
    },
    //第一个弹窗重置查询
    firstSearchReset() {
      this.firstQueryParams = {
        no: '',//入库单号
        sourceBase: '',//基地
        sourceGreenhouse: '',//地块
        storehouse: '',//冷库
        sDate: '',//开始时间
        eDate: ''//结束时间
      }
      this.firstSearch()
    },
    //第一个弹窗中根据条件查询
    firstSearch() {
      this.firstPagination.current = 1
      this.firstModalQuery()
    },
    //获取第一个弹窗中表格的数据
    firstModalQuery() {
      this.firstLoading = true
      this.firstDataSource.length = 0
      let params = {
        pageNo: this.firstPagination.current,
        pageSize: this.firstPagination.pageSize
      }
      params = { ...this.firstQueryParams, ...params }
      getFlowerTagInMain2(params).then(res => {
        if (res.success) {
          this.firstPagination.total = res.result.total
          this.firstDataSource = res.result.records
        } else {
          this.$message.error(res.message)
        }
        this.firstLoading = false
      })
    },
    //第一个弹窗中的分页
    firstPaginationChange(pagination) {
      this.firstPagination.current = pagination.current
      this.firstPagination.pageSize = pagination.pageSize
      this.firstModalQuery()
    },
    //第一个弹窗表格中单选的change
    firstSelectChange(selectedRowKeys, selectionRows) {
      this.firstSelectedRowKeys = selectedRowKeys//如果不加这两行的话无法单选
      this.firstSelectionRows = selectionRows
      this.adjustOrder = this.firstSelectionRows[0]//所选择要进行调整的订单
    },
    //获取账期列表
    getPeriod() {
      period().then(res => {
        this.periodList = res
      })
    },
    //账期单选的change
    periodChange(e) {
      this.firstSelectionRows.period = e.target.value
    },
    //第一个弹窗中的点击确定按钮
    firstModalOk() {
      if (this.firstSelectionRows.length === 0) {
        this.$message.error('请先选择要进行调整的订单！')
      } else {
        this.addOrEditFlag = 'add'
        this.secondModalTitle = '新增批量入库调整单'
        this.secondModalDisable = false
        this.queryFlowerTagInMain2Detail(this.adjustOrder.id)
        this.firstVisible = false//关闭第一个弹窗
        this.secondVisible = true//打开第二个弹窗
      }
    },
    //第一个弹窗中的点击取消按钮
    firstModalCancel() {
      this.firstVisible = false
    },
    //获取第二个弹窗中表格的数据
    secondModalQuery(id) {
      this.secondLoading = true
      let params = {
        id: id
      }
      getFlowerAdjustTagInMainDetailByMainId(params).then(res => {
        if (res.success) {
          this.secondDataSource = res.result
        } else {
          this.$message.error(res.message)
        }
        this.secondLoading = false
      })
    },
    queryFlowerTagInMain2Detail(id) {//获取第一个弹窗中所选择的批量入库单中子表的数据
      let params = {
        id: id
      }
      getFlowerTagInMain2DetailByMainId(params).then(res => {
        if (res.success) {
          this.tagInMain2DataSource = res.result
          this.tagInMain2DataSource.map(item => {
            item.uuid = getUuid()
            item.tagNum = ''
            delete item.id
            delete item.mainid
            delete item.createTime
            delete item.createUser
            delete item.updateTime
            delete item.updateUser
          })
        } else {
          this.$message.error(res.message)
        }
      })
    },
    secondModalFinanceSure() {// 第二个弹窗中的财务审核
      this.secondLoading = true
      this.secondModalFinanceSureButtonLoading = true
      flowerAdjustTagInMainFinanceSure({ id: this.adjustOrder.id }).then(res=>{
        if (res.success) {
          this.$message.success('财务审核成功！')
          this.queryFlowerAdjustTagInMain()
          this.secondVisible = false
        } else {
          this.$message.error(res.message)
        }
        this.secondLoading = false
        this.secondModalFinanceSureButtonLoading = false
      })

    },
    secondModalFinanceCancelSure() {// 第二个弹窗中的撤销财务审核
      this.secondLoading = true
      this.secondModalFinanceSureButtonLoading = true
      flowerAdjustTagInMainFinanceCancelSure({ id: this.adjustOrder.id }).then(res=>{
        if (res.success) {
          this.$message.success('撤销财务审核成功！')
          this.queryFlowerAdjustTagInMain()
          this.secondVisible = false
        } else {
          this.$message.error(res.message)
        }
        this.secondLoading = false
        this.secondModalFinanceSureButtonLoading = false
      })

    },
    varietyDelete(id) {// 第二个弹窗中删除商品
      const dataSource = [...this.secondDataSource]
      this.secondDataSource = dataSource.filter(item => item.varietyCode !== id)
    },
    varietyModalShow() {//打开第三个弹窗(商品选择的弹窗)
      /*设置下面这两行是为了处理再次打开商品选择弹窗时，
      还会在界面上保留上次选择商品而实际上没有选择任何商品的问题*/
      this.varietySelectedRowKeys.length = 0
      this.varietySelectionRows.length = 0

      this.selectVariety = []
      this.varietyPagination = {// 商品选择弹窗中表格的分页
        total: 0,
        current: 1,
        pageSize: 10,//每页中显示10条数据
        showSizeChanger: false,
        showQuickJumper: true,
        showTotal: total => `共有 ${total} 条数据`  //分页中显示总的数据
      }
      this.queryVarietyParams = {// 商品选择弹窗的查询
        name: '',
        code: ''
      }
      //this.queryFlowerVarietys()
      this.varietyVisible = !this.varietyVisible
    },
    searchFlowerVarietys() {//根据条件查询第三个弹窗(商品选择的弹窗)中的商品列表
      this.varietyPagination.current = 1
      this.queryFlowerVarietys()
    },
    queryFlowerVarietys() {//获取第三个弹窗(商品选择的弹窗)中的商品列表
      this.selectVarietyLoading = true
      let params = {
        pageNo: this.varietyPagination.current,
        pageSize: this.varietyPagination.pageSize
      }
      params = { ...this.queryVarietyParams, ...params }
      getVarietyList(params).then(res => {
        if (res.success) {
          this.selectVarietyDataSource = res.result
          this.selectVarietyDataSource.map(item => {
            item.variety = item.name
            item.varietyCode = item.code
          })
          // 将varietyNumber封装进selectVarietyDataSource中的各用户对象中,为第三个弹窗(商品选择的弹窗)中table的key使用
          if (this.varietyPagination.current > 1) {
            this.varietyNumber = (this.varietyPagination.current - 1) * 10 + 1
          } else {
            this.varietyNumber = 1
          }
          for (let item of this.selectVarietyDataSource) {
            item.varietyNumber = this.varietyNumber
            this.varietyNumber += 1
          }
        } else {
          this.$message.error(res.message)
        }
        this.selectVarietyLoading = false
      })
    },
    varietyPaginationChange(pagination) {
      this.varietyPagination = pagination
      this.queryFlowerVarietys()
    },
    onVarietySelectChange(selectedRowKeys, selectionRows) {
      this.varietySelectedRowKeys = selectedRowKeys// 如果不加这两行的话无法多选
      this.varietySelectionRows = selectionRows
    },
    varietyOnSelect(record, selected, selectedRows) {//选择/取消选择某个商品的回调
      if (selected) {//如果是选择某个商品的操作，就将所选择的商品添加进selectVariety
        this.selectVariety.push(record)
      } else {//如果是取消选择某个商品的操作，则从selectVariety中删除相应的商品
        this.selectVariety = this.selectVariety.filter(item => item.varietyNumber !== record.varietyNumber)
      }
    },
    varietyOnSelectAll(selected, selectedRows, changeRows) {//选择/取消选择所有商品的回调
      if (selected) {//如果是选择所有商品的操作，就将所选择的商品添加进selectVariety
        this.selectVariety = [...this.selectVariety, ...changeRows]
      } else {//如果是取消选择所有商品的操作，则从selectVariety中删除相应的商品
        for (let i of changeRows) {
          this.selectVariety = this.selectVariety.filter(item => item.varietyNumber !== i.varietyNumber)
        }
      }
    },
    varietyAdd() {//第三个弹窗(商品选择的弹窗)中的新增商品
      let varietyFlag = false//第二个弹窗表格中的商品是否包含新添加的商品
      outer:
        for (let item1 of this.secondDataSource) {
          for (let item2 of this.selectVariety) {
            if (item1.varietyCode === item2.varietyCode) {
              this.$message.error(`该入库调整单的商品中已有商品名称为:${item1.variety}，规格为:${item1.packingSpecifications}的商品`)
              varietyFlag = true
              break outer//跳出外层循环
            }
          }
        }
      if (!varietyFlag) {
        for (let item of this.selectVariety) {
          this.secondDataSource.push(item)
        }
      }
      this.varietyVisible = !this.varietyVisible
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less'
</style>